using System;
using System.Collections.Generic;
using EntryExit.Utils;
using System.Data;

namespace EntryExit
{
    public class Global_asax : System.Web.HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            DevExpress.Web.ASPxClasses.ASPxWebControl.CallbackError += new EventHandler(Application_Error);
            ConfigMgr.Instance.LoadConfigValues();
        }

        void Application_End(object sender, EventArgs e)
        {
        }

        void Application_Error(object sender, EventArgs e)
        {
        }

        void Session_Start(object sender, EventArgs e)
        {
            string windowsUserName = string.Empty;
            if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
            {
                windowsUserName = System.Web.HttpContext.Current.User.Identity.Name;

                if (windowsUserName.Contains(@"\\"))
                {
                    windowsUserName = windowsUserName.Split(new string[] { @"\\" }, StringSplitOptions.RemoveEmptyEntries)[1];
                }
                else if (windowsUserName.Contains(@"//"))
                {
                    windowsUserName = windowsUserName.Split(new string[] { @"//" }, StringSplitOptions.RemoveEmptyEntries)[1];
                }
                else if (windowsUserName.Contains(@"\"))
                {
                    windowsUserName = windowsUserName.Split(new string[] { @"\" }, StringSplitOptions.RemoveEmptyEntries)[1];
                }
                else if (windowsUserName.Contains(@"/"))
                {
                    windowsUserName = windowsUserName.Split(new string[] { @"/" }, StringSplitOptions.RemoveEmptyEntries)[1];
                }
            }
            else
            {
                windowsUserName = ConfigMgr.Instance.GetValue(ConfigKeys.CNFG_TEST_USER);
            }

            try
            {
                EmployeeManager empManagerContext = new EmployeeManager();
                string currentUserLPN = empManagerContext.GetLPNForWindowsUserName(windowsUserName);

                Employee currentEmployee = empManagerContext.GetEmployee(currentUserLPN);
                List<Employee> selectedEmployees = new List<Employee>();
                //selectedEmployees.Add(currentEmployee);
                EmployeeTree employeeTreeData = empManagerContext.GetEmployeeTreeData(currentEmployee);
                bool IS_Supervisor = false;
                bool isCurrentUserAdmin = false;
                string[] adminUsersConfigured = ((Convert.ToString(ConfigMgr.Instance.GetValue(ConfigKeys.CNFG_ADMIN_USERS))).Split(new char[] { ';' }));
                foreach (string user in adminUsersConfigured)
                {
                    if (user == currentEmployee.LPN)
                    {
                        isCurrentUserAdmin = true;
                        break;
                    }
                }
                DataTable dt = empManagerContext.GetSubEmployeesForAuthorityLPNVal(currentUserLPN);
                if (dt.Rows.Count > 0)
                {
                    IS_Supervisor = true;
                }
           
            Session[SessionKeys.CURRENT_EMPLOYEE] = currentEmployee;
            Session[SessionKeys.ORIGINAL_CURRENT_EMPLOYEE] = currentEmployee;
            Session[SessionKeys.SELECTED_EMPLOYEES] = selectedEmployees;
            Session[SessionKeys.MANAGER_CONTEXT] = empManagerContext;
            Session[SessionKeys.IS_ADMIN] = isCurrentUserAdmin;
            Session[SessionKeys.TREE_DATA] = employeeTreeData;
            Session[SessionKeys.SELECTED_EMP_LPN] = currentEmployee.LPN;
            Session[SessionKeys.IS_QUEENPERSONATED] = false;
            Session[SessionKeys.IS_Supervisor] = IS_Supervisor;
            }
            catch (Exception ex)
            {
                Logger.Error("Page : Global.asax.cs, Event : Session_Start", ex);
                Response.Redirect("Error.aspx");
            }
        }

        void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer 
            // or SQLServer, the event is not raised.
        }
    }
}